<?php 

require_once("form/test.php");

class SimpleForm extends Form {
	
	public function __construct(){
		parent::__construct(array(
			"name" => new Field(),
		));
	}
	
}

class FormTest extends Test {
	
	public function run(){
		$form = new SimpleForm();
		$this->tags($form);
		$this->invalid($form);
		$this->clean($form);
		$this->valid($form);
	}
	
	private function tags($form){
		$this->assertEquals($form->open(), "<form method=\"post\">");
		$this->assertEquals($form->close(), "</form>");
	}
	
	private function invalid($form){
		$this->assertEquals(FALSE, $form->isValid(), "Form is not valid");
		$this->assertTrue(in_array("name", $form->errors), "Name contains error");
		$this->assertTrue($form->containsError("name"), "Name contains error");
		$this->assertEmpty($form->getValue("name"));
	}
	
	private function clean($form){
		$form->clean();
		$this->assertFalse(in_array("name", $form->errors), "Name does NOT contain error anymore");
		$this->assertFalse($form->containsError("name"), "Name does NOT contain error anymore");
		$this->assertEmpty($form->getValue("name"));
	}
	
	private function valid($form){
		$form->populate(array("name" => "My name is Verta"));
		$this->assertTrue($form->isValid(), "Form valid");
		$this->assertFalse(in_array("name", $form->errors), "Name does NOT contain error cause it's valid");
		$this->assertFalse($form->containsError("name"), "Name does NOT contain error cause it's valid");
		$this->assertNotEmpty($form->getValue("name"));
		$this->assertEquals($form->getValue("name"), "My name is Verta");
	}
	
}

$test = new FormTest();
$test->run();